package org.example.message.生产;

import lombok.SneakyThrows;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * @Auther:Liu
 * @Testname:yiBu
 * @Date:2025/8/22 14:59
 */
public class yiBu {
    @SneakyThrows
    public static void main(String[] args) {
        // 1.创建消息生产者 producer，并指定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("producer_group_a");
        // 2.指定 NameSrv 地址
        producer.setNamesrvAddr("192.168.10.88:9876");
        // 3.启动 producer
        producer.start();
        // 4.创建消息对象，指定 Topic、Tag 和消息体
        for (int i = 0; i < 10; i++) {
            String topic = "topic_a";
            String tag = "tag_a"+i;
            String body = "body_a"+i;
            Message message = new Message(topic, tag, body.getBytes());
            // 5.发送消息
           producer.send(message,new SendCallback() {

               @Override
               public void onSuccess(SendResult sendResult) {
                   System.out.println(sendResult);
               }

               @Override
               public void onException(Throwable throwable) {
                   System.out.println("发送失败");
               }
           });
        }

        // 6.关闭生产者 producer
        Thread.sleep(1000);
        producer.shutdown();
    }
}
